﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Pg.BioMedics.SDR.Services.Thesaurus
{
    /// <summary>
    /// Hosts extension methods for Thesaurus
    /// </summary>
    internal static class ThesaurusExtensions
    {
        #region Extension methods

        /// <summary>
        /// Find list of menings for given word basing on the thesaurus
        /// </summary>
        /// <param name="word">Single word</param>
        /// <returns>List of meanings</returns>
        internal static Meaning[] FindMeanings(this string word)
        {
            return Thesaurus.Instance.GetMeanings(word);
        }

        /// <summary>
        /// Extension method allowing to find all 
        /// </summary>
        /// <param name="word"></param>
        /// <returns></returns>
        internal static string[] FindAllSynonyms(this string word)
        {
            List<string> synonyms = new List<string>();
            foreach (Meaning meaning in Thesaurus.Instance.GetMeanings(word))
                synonyms.AddRange(meaning.Synonyms);

            return synonyms.ToArray();
        }

        /// <summary>
        /// Checks if goven word is a stop word
        /// </summary>
        /// <param name="word">a word</param>
        /// <returns>true - if word is a stop word</returns>
        internal static bool IsStopWord(this string word)
        {            
            return Thesaurus.Instance.IsStoppingWord(word);
        }

        /// <summary>
        /// Checks if given text conteins stop words
        /// </summary>
        /// <param name="text">simple text</param>
        /// <returns>true - if text conteins stop words</returns>
        internal static bool ContainsStopWords(this string text)
        {
            if (String.IsNullOrEmpty(text))
                return false;

            return ((from word in Thesaurus.Instance.GetWords(text)
                     where Thesaurus.Instance.IsStoppingWord(word)
                     select word).Count() > 0);
        }

        /// <summary>
        /// Removes all stop words from the text
        /// </summary>
        /// <param name="text">Text with words</param>
        /// <returns>Text without stop words</returns>
        internal static string RemoveStopWords(this string text)
        {
            if (String.IsNullOrEmpty(text))
                return text;

            IEnumerable<string> stopWords = (from word in Thesaurus.Instance.GetWords(text)
                                             where Thesaurus.Instance.IsStoppingWord(word)
                                             select word);

            string result = text.ToLower();
            foreach (string stopWord in stopWords)
                result = result.Replace(stopWord, String.Empty);

            return result;
        }

        #endregion
    }
}
